package com.vrv.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.vrv.common.Constants;

/**
 * <ul>
 * 坐标类型说明
 * <li>返回国测局经纬度坐标系 coor=gcj02</li>
 * <li>返回百度墨卡托坐标系 coor=bd09</li>
 * <li>返回百度经纬度坐标系 coor=bd09ll</li>
 * </ul
 *
 * @author 曹保利
 *
 */
public class LocationActivity extends Activity {

    /* 百度地图开发者key */
    private static String mapKey = "7777feeaa8dc415f2451f288a3b73eab";
    private LocationClient mLocationClient = null;
    private MyLocationListenner myListener = new MyLocationListenner();

    private CheckBox mGpsCheck;
    private CheckBox mPriorityCheck;
    private TextView locationTextView;
    private TextView poinfoTextView;
    private Button siginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        mGpsCheck = (CheckBox) findViewById(R.id.gpsCheck);
        mPriorityCheck = (CheckBox) findViewById(R.id.priorityCheck);
        locationTextView = (TextView) findViewById(R.id.locationTextView);
        poinfoTextView = (TextView) findViewById(R.id.poinfoTextView);
        siginBtn = (Button) findViewById(R.id.siginBtn);

        mLocationClient = new LocationClient(this);
        mLocationClient.setAK(mapKey);// 这里的AK和应用签名包名绑定
        mLocationClient.registerLocationListener(myListener);

        siginBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setLocationOption();
                mLocationClient.start();
                mLocationClient.requestLocation();
            }
        });

    }

    // 设置相关参数
    private void setLocationOption() {
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(mGpsCheck.isChecked());
        option.setCoorType("bd09ll");
        option.setServiceName("com.baidu.location.service_v2.9");
        option.setPoiExtraInfo(false);// 设置是否需要POI的电话地址等详细信息
        option.setAddrType("all");
        option.setScanSpan(3000);
        if (mPriorityCheck.isChecked()) {
            option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
        } else {
            option.setPriority(LocationClientOption.GpsFirst); // 不设置，默认是gps优先
        }
        option.setPoiNumber(10);
        option.disableCache(true);
        mLocationClient.setLocOption(option);
    }

    /**
     * 监听函数，有更新位置的时候，格式化成字符串，输出到屏幕中
     */
    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null)
                return;
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\n省：");
                sb.append(location.getProvince());
                sb.append("\n市：");
                sb.append(location.getCity());
                sb.append("\n区/县：");
                sb.append(location.getDistrict());
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }
            sb.append("\nsdk version : ");
            sb.append(mLocationClient.getVersion());
            sb.append("\nisCellChangeFlag : ");
            sb.append(location.isCellChangeFlag());

            Log.i(Constants.TAG, sb.toString());
            locationTextView.setText(sb.toString());
        }

        @Override
        public void onReceivePoi(BDLocation poiLocation) {
            if (poiLocation == null) {
                return;
            }
            StringBuffer sb = new StringBuffer(256);
            sb.append("Poi time : ");
            sb.append(poiLocation.getTime());
            sb.append("\nerror code : ");
            sb.append(poiLocation.getLocType());
            sb.append("\nlatitude : ");
            sb.append(poiLocation.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(poiLocation.getLongitude());
            sb.append("\nradius : ");
            sb.append(poiLocation.getRadius());
            if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(poiLocation.getAddrStr());
            }
            if (poiLocation.hasPoi()) {
                sb.append("\nPoi:");
                sb.append(poiLocation.getPoi());
            } else {
                sb.append("noPoi information");
            }

            poinfoTextView.setText(sb.toString());
        }
    }
}
